//
//  HeaderView.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HeaderView.h"
#import "ShopModel.h"
#import "CenterCollectionViewCell.h"
#import "ShougongViewController.h"
#import "AppDelegate.h"
#import "ShougongHotViewController.h"
#import "BuyViewController.h"
@interface HeaderView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)NSMutableArray *dateArr;//数据数组
@property (nonatomic, retain)UICollectionView *butcoll;//collectionView
@property (nonatomic, retain)UICollectionViewFlowLayout *butlayout;//布局
@end
@implementation HeaderView

- (void)dealloc
{
    [_dateArr release];
    [_butcoll release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creat];
    }
    return self;
}
#pragma mark - collectionView

- (void)creat
{
    //数据
    [self data];
    //layout 布局
    self.butlayout = [[UICollectionViewFlowLayout alloc]init];
    //行列边距
    self.butlayout.minimumLineSpacing = 0;
    self.butlayout.minimumInteritemSpacing = 0;
    //Item大小
    self.butlayout.itemSize = CGSizeMake((self.frame.size.width- 20) / 4, 140);
    //滑动方向
    
    self.butlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //collectionView的创建
    self.butcoll = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 150) collectionViewLayout:_butlayout];
    self.butcoll.backgroundColor = [UIColor whiteColor];//背景颜色
    //代理
    self.butcoll.dataSource = self;
    self.butcoll.delegate = self;
    [self addSubview:self.butcoll];//添加
    //重用池
    [_butcoll registerClass:[CenterCollectionViewCell class] forCellWithReuseIdentifier:@"butcell"];
    [_butcoll release];
    
}
#pragma mark - collectionView必须实现的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CenterCollectionViewCell *butcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"butcell" forIndexPath:indexPath];
    ShopModel *s = _dateArr[indexPath.row];
    [butcell.bgimg sd_setImageWithURL:[NSURL URLWithString:s.pic]];
//    butcell.backgroundColor = [UIColor redColor];
    butcell.namelabel.text = s.name;
    return butcell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dateArr.count;
    //    return 10;
}
#pragma mark - 数据解析
- (void)data
{
    NSString *str = @"http://m.shougongke.com/index.php?c=Shiji&a=product&vid=15";
    [AFNnetWork GETwithURL:str completion:^(id result) {
     NSArray *arr = result[@"data"][@"hot"];
     self.dateArr = [ShopModel modelHandlerWithArray:arr];
     [_butcoll reloadData];
 }];
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];

    if (indexPath.item == 0) {
        ShougongViewController *col = [[ShougongViewController alloc]init];

        [app.secondnavi pushViewController:col animated:YES];;
    }
    if (indexPath.item == 1) {
        ShougongHotViewController *hot = [[ShougongHotViewController alloc]init];
        [app.secondnavi pushViewController:hot animated:YES];
    }
    if (indexPath.item == 2) {
        BuyViewController *buy = [[BuyViewController alloc]init];
        [app.secondnavi pushViewController:buy animated:YES];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
